﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;

namespace DataDesign.MPP.Utilities
{
    public class XPathDataSet
    {
        public XPathNavigator XPathNavigator { get; private set; }
        public XmlNamespaceManager NamespaceManager { get; private set; }

        public XPathDataSet() { }
        public XPathDataSet(System.Data.DataSet dataSet)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(dataSet.GetXml().Replace(":" + Constant.PREFIX, ""));
            XPathNavigator = doc.CreateNavigator();

            NamespaceManager = new XmlNamespaceManager(XPathNavigator.NameTable);
            NamespaceManager.AddNamespace(Constant.PREFIX, dataSet.Namespace);
        }

        public string SelectSingleNodeValue(string xpath)
        {
            XPathNavigator node = XPathNavigator.SelectSingleNode(xpath, NamespaceManager);
            if (node != null)
                return node.Value;
            return null;
        }        
    }
}
